#====================================================
#====================================================
#====================================================
#====================================================
#EWX SDK三方库模式需要提前配置SDK的路径
if(NOT EWX_USE_SDK_MODE)
	return()
endif()
#=============================================================================
#设定环境SDK环境目录
#这里可以进行选择处理
set(EWX_USE_SDK_TYPE "NULL" CACHE STRING 
	    "3RD Party Root Path Location Type Selection...")
set_property(CACHE EWX_USE_SDK_TYPE PROPERTY STRINGS 
		"NULL" 
		"ENV[EWX_SDK_ROOT]"
		"UserPath"
)
message(NOTICE "EWX_USE_SDK_TYPE = ${EWX_USE_SDK_TYPE}")
IF(EWX_USE_SDK_TYPE STREQUAL "ENV[EWX_SDK_ROOT]")
	COMMON_CHECK_ENV_PATH(check_ok ENV EWX_SDK_ROOT)
	IF(NOT check_ok)
		message(FATAL_ERROR "No Env Name With EWX_SDK_ROOT")
	ENDIF()
	COMMON_GET_ENV(env_EWX_SDK_ROOT EWX_SDK_ROOT)
	#message(FATAL_ERROR ${env_EWX_SDK_ROOT})
	SET(EWX_SDK_ROOT ${env_EWX_SDK_ROOT} CACHE PATH "Select the EWX_SDK Root Path...")
ELSEIF(EWX_USE_SDK_TYPE STREQUAL "UserPath")
	SET(EWX_SDK_ROOT "" CACHE PATH "Select the EWX_SDK Root Path...")
	IF(NOT EXISTS ${EWX_SDK_ROOT})
		message(FATAL_ERROR "No EWX_SDK_ROOT To Find EWX_SDK")
	endif()
ELSE()
	message(FATAL_ERROR "Must Select 3RD Location Type EWX_USE_SDK_TYPE = ${EWX_USE_SDK_TYPE}")
ENDIF()

##############################
SET(EWX_DEBUGER_EXEC_PATH_ROOT ${EWX_SDK_ROOT}/bin CACHE PATH "" FORCE)
if(NOT EXISTS "${EWX_DEBUGER_EXEC_PATH_ROOT}")
	message(FATAL_ERROR "Invalide SDK Binary Path: EWX_DEBUGER_EXEC_PATH_ROOT=${EWX_DEBUGER_EXEC_PATH_ROOT}")
endif()

#=====================================================
#查找包处理
find_package(EWX REQUIRED CONFIG 
	COMPONENTS 
		util
	NO_DEFAULT_PATH PATHS ${EWX_SDK_ROOT}
)

#根据SDK版本来确定插件的编辑条件
IF("v${EWX_KERNEL_VERSION}" STREQUAL "v2.6.0")
	add_definitions(-DEWX_KERNEL_260)
ELSEIF("v${EWX_KERNEL_VERSION}" STREQUAL "v2.9.0")
	add_definitions(-DEWX_KERNEL_290)
ELSE()
	message(FATAL_ERROR "No Find EWX_KERNEL_VERSION To Config SDK Conditions...")
ENDIF()
#=====================================================
#=====================================================
#=====================================================
#=============================================================================
#需要指定三方库的根目录
#三方库扩展
#此宏可以产生PROJECT_3RDPARTY_DIR变量用于作为三方库的目录
#MACRO_USE_WITH_3RD_PARTY_TYPE()

#增加查找目录
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${EWX_SDK_ROOT}/CMakeModules)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${EWX_SDK_ROOT}/CMakeModules/EWX_SDK_3RD)

#=============================================================================
IF("v${EWX_KERNEL_VERSION}" STREQUAL "v2.6.0")
	IF(WIN32)
		SET(DEPENDS_PKGS ${DEPENDS_PKGS} "qt-5.12.9")
		SET(DEPENDS_PKGS ${DEPENDS_PKGS} "osg-3.6.3")
		SET(DEPENDS_PKGS ${DEPENDS_PKGS} "osgearth-2.10.1")
		SET(DEPENDS_PKGS ${DEPENDS_PKGS} "sqlite-3.32.3")
	ELSE()

	ENDIF()
ELSEIF("v${EWX_KERNEL_VERSION}" STREQUAL "v2.9.0")
	IF(WIN32)
		SET(DEPENDS_PKGS ${DEPENDS_PKGS} "qt-5.12.9")
		SET(DEPENDS_PKGS ${DEPENDS_PKGS} "osg-3.6.3")
		SET(DEPENDS_PKGS ${DEPENDS_PKGS} "osgearth-2.10.1")
		SET(DEPENDS_PKGS ${DEPENDS_PKGS} "sqlite-3.32.3")
	ELSE()
		
	ENDIF()
ENDIF()
ewx_3rd_support_pkgs(${CMAKE_CURRENT_SOURCE_DIR}
                       ${DEPENDS_PKGS})   
#=============================================================================
#这里提前定义系统需要的依赖关系变量
#通过此模式决定查找库的依赖关系
#也定义了系统使用的CBB变量
#=======================
#基础CBB库[IMPORTANT][此模块库不应乱改]
SET(__ewx_tools_libs
	util
	util_script
	geodata
	packetio
	dis
	tracking_filters
	genio
	
)
#=======================
#核心CBB库[IMPORTANT][此模块库不应乱改]
set(__ewx_core_libs
	wsf
	wsf_util
)

#=======================
#p6dof库
set(__ewx_p6dof_libs )
#=======================
#vespatk库
set(__ewx_vespatk_libs
	vespatk
	vespatk_qt
)
#=======================

set(__ewx_wsf_exts
	#为了cmake过，暂时放在此处
	wsf_mtt
	wsf_nx
	wsf_l16
	wsf_ripr
	wsf_weapon_server

	#===wsf相关库===
	wsf_space
	wsf_parser
	wsf_mil_parser
	wsf_mil
	sensor_plot_lib
	wsf_cyber
	wsf_spaceg
	wsf_grammar_check
)

#其它未分类的CBB库
set(__ewx_other_libs
	#===与显示与界面相关的库===
	utilosg
	utilqt
)

set(__ewx_wkf_libs
 	wkf
 	wkf_common
)

#不同内核的组成不同
IF("v${EWX_KERNEL_VERSION}" STREQUAL "v2.6.0")
	set(__ewx_core_libs ${__ewx_core_libs}
		wsf_xio
	)

	set(__ewx_tools_libs ${__ewx_tools_libs} 	
		post_processor_lib
	)

	set(__ewx_p6dof_libs ${__ewx_p6dof_libs}
		p6dof
		wsf_p6dof
	)

	set(__ewx_wsf_exts ${__ewx_wsf_exts}
		#wsf_ex
		wsf_simdis
	)

	set(__ewx_other_libs ${__ewx_other_libs}

	)

	set(__ewx_wkf_libs ${__ewx_wkf_libs}
		results_lib
		wizard_core
		warlock_core
		warlock_extention
		ScenarioEdit
		#回放模块
		results_lib_withoutwkf
	)

ELSEIF("v${EWX_KERNEL_VERSION}" STREQUAL "v2.9.0")
	set(__ewx_tools_libs ${__ewx_tools_libs} 	
		profiling
		post_processor_lib
		artificer
	)

	set(__ewx_wsf_exts
		${__ewx_wsf_exts}
		wsf_simdis
		wsf_p6dof
		wsf_six_dof
		brawler
		wsf_brawler
		mover_creator_lib
	)

	set(__ewx_other_libs ${__ewx_other_libs}

	)

	set(__ewx_wkf_libs ${__ewx_wkf_libs}
		wizard_core
		warlock_core
		mystic_lib
	)

	# 添加定义，在290SDK模式下有版本字符串的问题
	add_compile_definitions(PROMOTE_HARDWARE_EXCEPTIONS)
	#add_compile_definitions("$<IF:$<CONFIG:RelWithDebInfo>,PROMOTE_HARDWARE_EXCEPTIONS,>")
	#add_compile_definitions("$<IF:$<CONFIG:Debug>,PROMOTE_HARDWARE_EXCEPTIONS,$<IF:$<CONFIG:RelWithDebInfo>,PROMOTE_HARDWARE_EXCEPTIONS,>>")
ENDIF()

#=============================================================================
function(make_ewx_sys_varaible var_out)
	set(options)
	set(oneValueArgs)
	set(multiValueArgs LIBS)
	cmake_parse_arguments(_args "${options}" "${oneValueArgs}"
				"${multiValueArgs}" ${ARGN} )
	set(__libs__ ${_args_UNPARSED_ARGUMENTS} ${_args_LIBS})
	set(__tmplibs)
	foreach(lib ${__libs__})
		set(__tmplibs ${__tmplibs} EWX::${lib})
	endforeach()
	set(${var_out} ${__tmplibs} PARENT_SCOPE)
	message(STATUS "====>Make EWX Important Variable:\n  ${var_out} = [\n    ${__tmplibs}\n  ]")
endfunction()
#=========================================
#核心库的变量，用于后期方便使用
#工具库
make_ewx_sys_varaible(EWX_TOOLS_LIBS 
	LIBS 
		${__ewx_tools_libs}
)

#扩展库对象
make_ewx_sys_varaible(EWX_WSF_EXTS_LIBS 
	LIBS 
		${__ewx_wsf_exts}
)
#=========================================
#核心库
make_ewx_sys_varaible(_EWX_CORE_LIBS
	LIBS 
		${__ewx_tools_libs}
		${__ewx_core_libs} 
)
#=============================================================================
#通过宏处理相关的批处理查找模块问题
macro(EWX_FIND_PACKAGE)
	SET(options USE_WKF)
	SET(oneValueArgs)
	SET(multiValueArgs)

	cmake_parse_arguments(_ARGS 
			"${options}" 
			"${oneValueArgs}" 
			"${multiValueArgs}" 
			${ARGN}
	)

	IF("v${EWX_KERNEL_VERSION}" STREQUAL "v2.6.0")
		SET(__linked_library 
			#基础CBB库
			${__ewx_tools_libs}
			#核心CBB库
			${__ewx_core_libs}

			${__ewx_wsf_exts}

			#基他相关系统模块
			${__ewx_p6dof_libs}
			${__ewx_vespatk_libs}

			${__ewx_other_libs}
		)
	ELSEIF("v${EWX_KERNEL_VERSION}" STREQUAL "v2.9.0")
		SET(__linked_library 
			#基础CBB库
			${__ewx_tools_libs}
			#核心CBB库
			${__ewx_core_libs}

			${__ewx_wsf_exts}

			#基他相关系统模块
			${__ewx_p6dof_libs}
			${__ewx_vespatk_libs}

			${__ewx_other_libs}
		)
	ELSE()
		message(FATAL_ERROR "No Find EWX_KERNEL_VERSION To Config SDK Conditions...")
	ENDIF()

	#确定Qt的环境处理
	IF(_ARGS_USE_WKF)
		SET(__linked_library ${__linked_library} ${__ewx_wkf_libs})
	ENDIF()

	#查找包处理
	find_package(EWX REQUIRED CONFIG 
		COMPONENTS 
			${__linked_library}
		NO_DEFAULT_PATH PATHS ${EWX_SDK_ROOT}
	)
endmacro()
